package Dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Dao.Daoaddress;
import entity.Address;
import entity.Commodity;
import entity.Imgtype;
import utils.BaseDao;

public class Daoimpladdress extends BaseDao implements Daoaddress {

	@Override
	public List<Address> sel(int id) {
		String sql = "select * from address where uid = ?";
		Object[] object = {id};
		ResultSet set = super.executeQuery(sql, object);
		List<Address> list = new ArrayList<Address>();
		try {
			while(set.next()){
				Address add = new Address();
				add.setId(set.getInt(1));
				add.setName(set.getString(2));
				add.setAddress(set.getString(3));
				add.setPhone(set.getString(4));
				add.setUid(set.getInt(5));
				add.setAddressxiang(set.getString(6));
				add.setMoren(set.getInt(7));
				list.add(add);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public boolean add(Address add) {
		String sql = "insert into `xiangmu`.`address` (`name`, `address`, `phone`, `uid`,`addressxiang`,`moren`) values (?, ?, ?, ?,?,1);";
		Object[] object = {add.getName(),add.getAddress(),add.getPhone(),add.getUid(),add.getAddressxiang()};
		return super.executeUpdate(sql, object);
	}

	@Override
	public boolean addmo(Address add) {
		String sql = "insert into `xiangmu`.`address` (`name`, `address`, `phone`, `uid`,`addressxiang`,`moren`) values (?, ?, ?, ?,?,2);";
		Object[] object = {add.getName(),add.getAddress(),add.getPhone(),add.getUid(),add.getAddressxiang()};
		return super.executeUpdate(sql, object);
	}

	@Override
	public boolean updmo(int id,int uid) {
		String sql = "update `xiangmu`.`address` set `moren` = '1' where `uid` = ? ";
		Object[] object = {uid};
		boolean jia = super.executeUpdate(sql, object);
		boolean hao = false;
		if(jia){
			String sql1 = "update `xiangmu`.`address` set `moren` = '2' where `id` = ?";
			Object[] object1 = {id};
			hao = super.executeUpdate(sql1, object1);
		}
		return hao;
	}

	@Override
	public Address selmo(int id) {
		String sql = "select * from address where uid = ? and moren = 2";
		Object[] object = {id};
		ResultSet set = super.executeQuery(sql, object);
		Address add =null;
		try {
			while(set.next()){
				add = new Address();
				add.setId(set.getInt(1));
				add.setName(set.getString(2));
				add.setAddress(set.getString(3));
				add.setPhone(set.getString(4));
				add.setUid(set.getInt(5));
				add.setAddressxiang(set.getString(6));
				add.setMoren(set.getInt(7));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return add;
	}
	@Override
	public boolean addImgtype(Imgtype c) {
		String sql="insert into `imgtype` values(null,?,?,?)";
		Object[] params={c.getImg(),c.getType(),c.getShijian()};
		return super.executeUpdate(sql, params);
	}


	@Override
	public Imgtype Info(int id) {
		String sql ="select * from `Imgtype` where `id`=?";
		Object[] params ={id};
		ResultSet rs =super.executeQuery(sql, params);
		Imgtype c =null;
	   try {
		   while (rs.next()) {
				c =new Imgtype();
				c.setId(rs.getInt("id"));
			    c.setImg(rs.getString("img"));
			    c.setType(rs.getString("type"));
			    c.setShijian(rs.getTimestamp("shijian").toString());
			}
	} catch (SQLException e) {
		e.printStackTrace();
	}finally{
		try {
			super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
		return c;
	}


	@Override
	public boolean gedel(int id) {
		String sql="delete from `xiangmu`.`address` where `id` =?";
		Object[] cmh={id};
		return super.executeUpdate(sql,cmh);
	}

	@Override
	public boolean getupd(Address a) {
		String sql="update `xiangmu`.`address` set `name` =? , `address` =? , `phone` =?, `uid` =?, `addressxiang` =?, `moren` =1 where `id` =?";
		Object[] cmh={a.getName(),a.getAddress(),a.getPhone(),a.getUid(),a.getAddressxiang(),a.getId()};
		return super.executeUpdate(sql, cmh);
	}

	@Override
	public Address getone(int id) {
		String sql="select * from `address` where `id`=?";
		Object[] cmh={id};
		ResultSet set = super.executeQuery(sql, cmh);
		Address add=null;
		try {
			while(set.next()){
				add = new Address();
				add.setId(set.getInt(1));
				add.setName(set.getString(2));
				add.setAddress(set.getString(3));
				add.setPhone(set.getString(4));
				add.setUid(set.getInt(5));
				add.setAddressxiang(set.getString(6));
				add.setMoren(set.getInt(7));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return add;
	}

}
